package com.oto.service;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;


import org.springframework.stereotype.Service;

import com.oto.model.AccountDO;

/**
 * @author xueqiang.mi
 * @since 2010-12-18
 */
@Service
public class AccountService extends BaseService {

	public void add(AccountDO obj) {
		if (exist(obj.getName())) {
			return;
		}
		Date now = new Date();
		obj.setCrtDate(now);
		obj.setModDate(now);
		save(obj);
	}

	public void add(String name, String type) {
		AccountDO u = new AccountDO();
		u.setName(name);
		u.setType(type);
		add(u);
	}

	public boolean addFriend(String userName, String fridName) {
		PersistenceManager pm = PMF.get();
		try {
			AccountDO account = pm.getObjectById(AccountDO.class, userName);
			if (account != null) {
				account.getFriends().add(fridName);
				return true;
			}
		} finally {
			pm.close();
		}
		return false;
	}

	public boolean exist(String name) {
		try {
			return (get(AccountDO.class, name) != null);
		} catch (Exception e) {
			return false;
		}
	}

	public AccountDO get(String name) {
		try {
			return get(AccountDO.class, name);
		} catch (Exception e) {
			return null;
		}
	}

	public List<AccountDO> list() {
		return list(AccountDO.class);
	}
}
